/**
 * 
 */
package edu.bds;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/**
 * @author Basanta
 * 
 */
public class PSPDataExcelReader {

	public PSPDataExcelReader() {

	}

	private static List<List<String>> timeLogData = new ArrayList<List<String>>();

	/**
	 * 
	 * @param fileName
	 * @throws Exception
	 */
	public static void readXls(String fileName) throws Exception {

		// Each entries contain single row in comma separated format as Phase,
		// Start Time, Delta, Interrupt
		
		//Reset the array
		timeLogData = new ArrayList<List<String>>();
		
		File file = null;
		FileInputStream fileInputStream = null;

		try {

			file = new File(fileName);
			if (file.exists()) {
				if (file.isFile()) {
					if (file.canRead()) {
						fileInputStream = new FileInputStream(fileName);
						HSSFWorkbook workbook = new HSSFWorkbook(
								fileInputStream);

						HSSFSheet sheet = workbook.getSheetAt(0);

						Iterator rowIterator = sheet.rowIterator();

						while (rowIterator.hasNext()) {
							HSSFRow row = (HSSFRow) rowIterator.next();
							if (row.getRowNum() > 2) {
								Iterator cellIterator = row.cellIterator();
								List<String> rowValue = new ArrayList<String>();
								while (cellIterator.hasNext()) {
									HSSFCell cell = (HSSFCell) cellIterator
											.next();
									if (cell.getColumnIndex() > 0
											&& cell.getColumnIndex() < 5) {
										rowValue.add(cell.toString().trim());
									}
								}
								timeLogData.add(rowValue);
							}
						}
					}
				}
			}
			fileInputStream.close();
		} catch (IOException e) {
			System.err.println("Input/Output error. Please check file.");
			e.printStackTrace();
		} finally {
			if (fileInputStream != null) {
				fileInputStream.close();
			}
		}
		if (file.exists()) {
			file.delete();
		}
		// showLogData(timeLogData);
	}

	public static String getSheetName(String fileName, int sheetNum)
			throws Exception {

		// Each entries contain single row in comma separated format as Phase,
		// Start Time, Delta, Interrupt

		File file = null;
		FileInputStream fileInputStream = null;
		String sheetName = "";
		try {
			file = new File(fileName);
			if (file.exists()) {
				if (file.isFile()) {
					if (file.canRead()) {
						fileInputStream = new FileInputStream(fileName);
						HSSFWorkbook workbook = new HSSFWorkbook(
								fileInputStream);

						HSSFSheet sheet = workbook.getSheetAt(sheetNum);

						sheetName = sheet.getSheetName();

					}
				}
			}
			fileInputStream.close();
		} catch (IOException e) {
			System.err.println("Input/Output error. Please check file.");
			e.printStackTrace();
		} finally {
			if (fileInputStream != null) {
				fileInputStream.close();
			}
		}
		return sheetName;
	}

	/**
	 * 
	 * @param logData
	 */
	@SuppressWarnings("unused")
	private static void showLogData(List<List<String>> logData) {
		System.out.println("LogData = size = " + logData.size());
		Iterator<List<String>> itr = logData.iterator();
		while (itr.hasNext()) {
			List<String> rowVal = itr.next();
			Iterator<String> rowValItr = rowVal.iterator();
			while (rowValItr.hasNext()) {
				System.out.println(rowValItr.next());

			}
		}
	}

	public List<List<String>> getTimeLogData() {
		return timeLogData;
	}
	
	public static void setTimeLogData(List<List<String>> timeLogData) {
		PSPDataExcelReader.timeLogData = timeLogData;
	}

}
